package com.ittest.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.ittest.domain.store.City;
import com.ittest.domain.store.Product;
import com.ittest.service.store.CityService;
import com.ittest.service.store.ProductService;
import com.ittest.service.store.impl.CityServiceImpl;
import com.ittest.service.store.impl.ProductServiceImpl;
import com.ittest.utils.BeanUtil;
import com.ittest.web.controller.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/store/product/*")
public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductServiceImpl();
    private CityService cityService = new CityServiceImpl();

    //开启和屏蔽
    protected void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取状态信息
        Integer productStatus = Integer.parseInt(req.getParameter("productStatus"));

        String id = req.getParameter("id");
        //service调用方法
        if (productStatus == 1){
            productService.open(id);
        }else{
            productService.close(id);
        }
        //重定向到分页查询操作
        resp.sendRedirect(req.getContextPath()+"/store/product/findAll");
    }

    //添加
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        Product product = new Product();
        Map<String, String[]> map = req.getParameterMap();
        Iterator<String> iter = map.keySet().iterator();
        while(iter.hasNext()){
            String key = iter.next();
            if("departureTime".equals(key)){
                iter.remove();
            }
        }
        try {
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String departureTime = req.getParameter("departureTime");
        Date date = sdf.parse(departureTime);
        product.setDepartureTime(date);

        //调用修改方法
        productService.add(product);
        //重定向到分页查询操作
        resp.sendRedirect(req.getContextPath()+"/store/product/findAll");
    }

    //跳转到新增页面
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询一个城市集合
        List<City> citys = cityService.findAll();
        req.setAttribute("citys",citys);
        //跳转到页面
        req.getRequestDispatcher("/pages/store/product/product-add.jsp").forward(req,resp);
    }

    //修改方法
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        Product product = new Product();
        Map<String, String[]> map = req.getParameterMap();
        Iterator<String> iter = map.keySet().iterator();
        while(iter.hasNext()){
            String key = iter.next();
            if("departureTime".equals(key)){
                iter.remove();
            }
        }
        try {
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String departureTime = req.getParameter("departureTime");
        Date date = sdf.parse(departureTime);
        product.setDepartureTime(date);

        //调用修改方法
        productService.update(product);
        //重定向到分页查询操作
        resp.sendRedirect(req.getContextPath()+"/store/product/findAll");
    }

    //跳转修改页面
    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        //根据id查询企业对象
        Product product = productService.findById(id);
        //查询一个城市集合
        List<City> citys = cityService.findAll();
        req.setAttribute("citys",citys);
        //保存数据到页面
        req.setAttribute("product",product);
        //跳转到页面
        req.getRequestDispatcher("/pages/store/product/product-update.jsp").forward(req,resp);
    }


    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //调用service方法
        productService.delete(id);
        //重定向到分页查询操作
        resp.sendRedirect(req.getContextPath()+"/store/product/findAll");
    }

    //查询
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page =1 ;
        int size =5 ;

        if (StringUtils.isNotBlank(req.getParameter("page"))){
            page=Integer.parseInt(req.getParameter("page"));
        }
        if (StringUtils.isNotBlank(req.getParameter("size"))){
            size=Integer.parseInt(req.getParameter("size"));
        }
        String sname = req.getParameter("sname");
        //调用service层,查询数据
        PageInfo pageInfo = productService.findAll(sname,page, size);
        //数据存到request域中
        req.setAttribute("size",size);
        req.setAttribute("page",pageInfo);
        //跳转到页面
        req.getRequestDispatcher("/pages/store/product/product-list.jsp").forward(req,resp);
    }
}
